package com.yds.longlinks.server;

import android.util.Log;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.net.InetSocketAddress;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 作者:     YDS
 * 创建时间: 2022/11/30 10:23
 * 页面描述:
 */
public class MinaServer {

    private static DefaultHandler defaultHandler;

    public static void start() {
        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        defaultHandler = new DefaultHandler();
        acceptor.setHandler(defaultHandler);
        acceptor.getSessionConfig().setReadBufferSize(1024 * 10);
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 1000 * 10);
        Log.i("YDSimon","Mina服务端：start");
        try {
            acceptor.bind(new InetSocketAddress(8686));
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("YDSimon","Mina服务端：Exception " + e.getMessage());
        }

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                defaultHandler.getSession().write("主动发送消息");
                Log.i("YDSimon", "主动发送消息");
            }
        };
        timer.schedule(task, 10 * 15000);
    }

    /**
     * 负责session对象的创建监听以及消息发送和接收的监听
     */
    private static class DefaultHandler extends IoHandlerAdapter {
        private IoSession mIoSession;

        public IoSession getSession() {
            return mIoSession;
        }

        @Override
        public void sessionCreated(IoSession session) throws Exception {
            super.sessionCreated(session);
            this.mIoSession = session;
            Log.i("YDSimon","Mina服务端：sessionCreated");
        }

        @Override
        public void sessionOpened(IoSession session) throws Exception {
            super.sessionOpened(session);
            Log.i("YDSimon","Mina服务端：sessionOpened");
        }

        @Override
        public void messageReceived(IoSession session, Object message) throws Exception {
            super.messageReceived(session, message);
            String content = message.toString();
            Date date = new Date();
            session.write(date.toString());
            Log.i("YDSimon","Mina服务端接收消息：" + content);

            defaultHandler.getSession().write("主动发送消息:"+content);
        }

        @Override
        public void messageSent(IoSession session, Object message) throws Exception {
            super.messageSent(session, message);
        }

        @Override
        public void sessionClosed(IoSession session) throws Exception {
            super.sessionClosed(session);
            Log.i("YDSimon","Mina服务端：sessionClosed");
        }

        @Override
        public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
            super.sessionIdle(session, status);
        }
    }
}
